package com.ruoyi.emergencyplan.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.emergencyplan.dto.MaterialCategoryDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.emergencyplan.mapper.MaterialCategoryMapper;
import com.ruoyi.emergencyplan.domain.MaterialCategory;
import com.ruoyi.emergencyplan.service.IMaterialCategoryService;

/**
 * 物资类别Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-17
 */
@Service
public class MaterialCategoryServiceImpl implements IMaterialCategoryService 
{
    @Autowired
    private MaterialCategoryMapper materialCategoryMapper;

    /**
     * 查询物资类别
     * 
     * @param id 物资类别主键
     * @return 物资类别
     */
    @Override
    public MaterialCategory selectMaterialCategoryById(String id)
    {
        return materialCategoryMapper.selectMaterialCategoryById(id);
    }

    /**
     * 查询物资类别列表
     * 
     * @param materialCategory 物资类别
     * @return 物资类别
     */
    @Override
    public List<MaterialCategory> selectMaterialCategoryList(MaterialCategory materialCategory)
    {
        return materialCategoryMapper.selectMaterialCategoryList(materialCategory);
    }

    /**
     * 新增物资类别
     * 
     * @param materialCategory 物资类别
     * @return 结果
     */
    @Override
    public int insertMaterialCategory(MaterialCategory materialCategory)
    {
        return materialCategoryMapper.insertMaterialCategory(materialCategory);
    }

    /**
     * 修改物资类别
     * 
     * @param materialCategory 物资类别
     * @return 结果
     */
    @Override
    public int updateMaterialCategory(MaterialCategory materialCategory)
    {
        return materialCategoryMapper.updateMaterialCategory(materialCategory);
    }

    /**
     * 批量删除物资类别
     * 
     * @param ids 需要删除的物资类别主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCategoryByIds(String[] ids)
    {
        return materialCategoryMapper.deleteMaterialCategoryByIds(ids);
    }

    /**
     * 删除物资类别信息
     * 
     * @param id 物资类别主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCategoryById(String id)
    {
        return materialCategoryMapper.deleteMaterialCategoryById(id);
    }

    /**
     * 查询物资类别列表
     * @param materialCategory
     * @return
     */
    @Override
    public List<MaterialCategoryDto> getMaterialCategoryList(MaterialCategory materialCategory) {
        // 获取所有类别的列表
        List<MaterialCategoryDto> categoryList = materialCategoryMapper.getCategoryTree(materialCategory);
        // 以类别id为key，类别对象为value构建一个Map
        Map<String, MaterialCategoryDto> categoryMap = new HashMap<>();
        for (MaterialCategoryDto category : categoryList) {
            category.setChildren(new ArrayList<>()); // 初始化子类别列表
            categoryMap.put(category.getId(), category);
        }
        // 构建树形结构
        List<MaterialCategoryDto> categoryTree = new ArrayList<>();
        for (MaterialCategoryDto category : categoryList) {
            if (category.getParentId() != null && categoryMap.containsKey(category.getParentId())) {
                categoryMap.get(category.getParentId()).getChildren().add(category);
            } else {
                categoryTree.add(category);
            }
        }
        return categoryTree;
    }
}
